# -*- coding: utf-8 -*- 
"""
========================================================================================================================
@project : my-sanic
@file: authors
@Author: mengying
@email: 652044581@qq.com
@date: 2023/3/7 13:48
@desc: 
========================================================================================================================
"""
from sanic.views import HTTPMethodView
from utils.myDecorator import asyncInterface
from utils.myLog import SeqLog
from utils.myCache import Cache
from sanic.response import text
from sanic.response import json as JsonResponse
from utils.myResFormat import ResultJson, ResultCode

# 配置日志
seqLog = SeqLog().get_logger()

# 配置缓存
RedisClient = Cache().select('redis').current_db

# 数据库导入
from database.model.demo import StudentDao


# 类试图
class AsyncTestView(HTTPMethodView):

    # 异步装饰器
    @asyncInterface()
    async def get(self, request):
        # 方法一: 从app中获取cache实例
        cache = request.app.ctx.cache

        # 本地缓存(默认的使用本地缓存)
        await cache.current_db.set("name", "age")
        key = await cache.current_db.get("name")

        # 方法二:在py中声明全局实例
        await RedisClient.set("name2", "age2")
        key = await RedisClient.get("name2")

        # 数据库的使用
        id = await StudentDao().insert_one({"name": "age"})

        return JsonResponse(ResultJson(ret=ResultCode.SUCCESS, data=id).result)


class HealthView(HTTPMethodView):
    """服务的健康检查"""
    async def get(self, request):

        return text('I am health')